package com.hkfn.rfb.rfb.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.adapter.BaseRecyclerAdapter;
import com.hkfn.rfb.common.fragment.BaseFragment;
import com.hkfn.rfb.common.utils.DialogUtils;
import com.hkfn.rfb.common.utils.StringUtils;
import com.hkfn.rfb.common.utils.ToastUtils;
import com.hkfn.rfb.databinding.FRfbSystemSettingBinding;
import com.hkfn.rfb.rfb.adapter.RfbSystemSettingAdapter;
import com.hkfn.rfb.rfb.bean.RfbParamManager;
import com.hkfn.rfb.rfb.bean.RfbParamModel;
import com.hkfn.rfb.rfb.bean.RfbSystemSettingModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统配置
 */
public class RfbSystemSettingFragment extends BaseFragment {

    private BaseActivity activity;

    private Bundle bundle;

    private FRfbSystemSettingBinding fRfbSystemSettingBinding;

    private RfbSystemSettingAdapter rfbSystemSettingAdapter;
    private GridLayoutManager gridLayoutManager;

    private Dialog changeParamSettingDialog;
    private String upValue = "";
    private String downValue = "";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = getMyActivity();
        bundle = getArguments();
        fRfbSystemSettingBinding = FRfbSystemSettingBinding.inflate(inflater, container, false);
        initView();
        return fRfbSystemSettingBinding.getRoot();
    }

    private void initView() {
        gridLayoutManager = new GridLayoutManager(activity, 5);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fRfbSystemSettingBinding.rlvSystemSetting.setLayoutManager(gridLayoutManager);
        rfbSystemSettingAdapter = new RfbSystemSettingAdapter(activity);
        fRfbSystemSettingBinding.rlvSystemSetting.setAdapter(rfbSystemSettingAdapter);

        initData();
    }

    private void initData() {
        List<RfbSystemSettingModel> list = new ArrayList<>();
        // 温度参数
        RfbSystemSettingModel modelT = new RfbSystemSettingModel("电池", "", "",
                "", "0", "0");
        list.add(modelT);
        for (int i = 0; i < RfbParamManager.batteryParamManager.size(); i++) {
            RfbParamModel rfbParamModel = RfbParamManager.batteryParamManager.get(i);
            RfbSystemSettingModel rfbSystemSettingModel = new RfbSystemSettingModel(rfbParamModel.getParamName(), rfbParamModel.getParamMaxValue(), rfbParamModel.getParamMinValue(),
                    rfbParamModel.getParamUnit(), "0", "1");
            list.add(rfbSystemSettingModel);
        }
        // 温度参数
        RfbSystemSettingModel modelT1 = new RfbSystemSettingModel("温度", "", "",
                "", "1", "0");
        list.add(modelT1);
        for (int i = 0; i < RfbParamManager.temperatureParamManager.size(); i++) {
            RfbParamModel rfbParamModel = RfbParamManager.temperatureParamManager.get(i);
            RfbSystemSettingModel rfbSystemSettingModel = new RfbSystemSettingModel(rfbParamModel.getParamName(), rfbParamModel.getParamMaxValue(), rfbParamModel.getParamMinValue(),
                    rfbParamModel.getParamUnit(), "1", "1");
            list.add(rfbSystemSettingModel);
        }
        // 气体参数
        RfbSystemSettingModel modelT2 = new RfbSystemSettingModel("气体", "", "",
                "", "2", "0");
        list.add(modelT2);
        for (int i = 0; i < RfbParamManager.gasParamManager.size(); i++) {
            RfbParamModel rfbParamModel = RfbParamManager.gasParamManager.get(i);
            RfbSystemSettingModel rfbSystemSettingModel = new RfbSystemSettingModel(rfbParamModel.getParamName(), rfbParamModel.getParamMaxValue(), rfbParamModel.getParamMinValue(),
                    rfbParamModel.getParamUnit(), "2", "1");
            list.add(rfbSystemSettingModel);
        }

        rfbSystemSettingAdapter.addAllDatas(list);

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                RfbSystemSettingModel rfbSystemSettingModel = list.get(position);
                if ("0".equals(rfbSystemSettingModel.getType())){
                    return 5;
                }else {
                    return 1;
                }
            }
        });

        rfbSystemSettingAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<RfbSystemSettingModel>() {
            @Override
            public void onItemClick(View v, int position, RfbSystemSettingModel data) {
                if ("0".equals(data.getType())){
                    return;
                }
                upValue = data.getMaxValue();
                downValue = data.getMinValue();
                changeParamSettingDialog = DialogUtils.createChangeParamSettingDialog(activity, data, new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        upValue = s.toString();
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                }, new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        downValue = s.toString();
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                },
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.closeDialog(changeParamSettingDialog);
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (StringUtils.isNumeric(upValue) && StringUtils.isNumeric(downValue) && Double.parseDouble(upValue) > Double.parseDouble(downValue)){
                            data.setMaxValue(upValue);
                            data.setMinValue(downValue);
                            rfbSystemSettingAdapter.notifyDataSetChanged();
                            DialogUtils.closeDialog(changeParamSettingDialog);
                        }else{
                            ToastUtils.showToast(activity, "数值设置错误");
                        }

                    }
                });
                DialogUtils.showDialog(changeParamSettingDialog);
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DialogUtils.closeDialog(changeParamSettingDialog);
    }
}
